 /********************************************************************************
* EJAF - Community survey main analysis (knowledge and facility utilization): 							
*
* 	Created: 12 August 2015
* 	Last updated: 19 October 2018 by Pascal
*
* 	This file:
* 		1. Conducts the final analyses for the main paper
* 		2. Data are first cleaned using the do-file crejpop01
*
*******************************************************************************/

//version 13.1
clear all
set more off
//qui set mem 300m
//qui set matsize 799
//qui set maxvar 8000
program drop _all

/******************************************
		SELECT Change global serverpath for different users
******************************************/

global serverpath  "xxx"

//capture log close
//log using "$serverpath/log files/crejafmain01.smcl", replace

use "$serverpath/crejpop01.dta", clear
/******************************************************************************						
		0. Creating the analytic dataset (remove individuals w/out street and ineligible)
******************************************************************************/
drop if ward == .

drop if currentpreg == 2 

/******************************************************************************						
		1. General statistics
******************************************************************************/
** generate new education variable for table 1 
generate education = . 
replace education = 0 if q25==0
replace education = 1 if q25==1
replace education = 2 if q25==2 | q25==3 
replace education = 3 if q25==4 | q25==5
label def newed 0 "< primary" 1 "primary" 2 "secondary" 3 "> secondary" 
tab education

** Create age group 
gen age_grp = . 
replace age_grp = 1 if age>=15 & age <20 & age !=.
replace age_grp = 2 if age>=20 & age <25 & age !=.
replace age_grp = 3 if age>=25 & age <30 & age !=.
replace age_grp = 4 if age>=30 & age <35 & age !=.
replace age_grp = 5 if age>=35 & age <40 & age !=.
replace age_grp = 6 if age>=40 & age !=.


** Checking number of missing observations for variables of interest
mdesc street 
mdesc ward q25 secondaryed age HBCevervisit facdel intendfacdel q12 q13 q14 q26 q29

gen timesincedel = q1 - q22
label var timesincedel "no. of days since delivery"
summ timesincedel
replace timesincedel = . if timesincedel < 0
summ timesincedel
tab timesincedel if timesincedel >= 720 & street != .

gen anctimes = 0 if q12 == 0
replace anctimes = q14 if anctimes == .

gen anc4more = 0 if anctimes != .
recode anc4more (0= 1) if anctimes >= 4
lab var anc4more "4 or more ANC visits during pregnancy"

** Table 1 (summary characteristics)
table1, by(intervention) vars(age contn \ age_grp cat \ education cat \ secondaryed cat \ q26 contn \ q29 contn \ timesincedel contn \ currentpreg cat) format(%2.1f) saving("$serverpath/table1.xls", replace)


**Do intervention areas receive more visits by HBCs?
	mdesc HBCevervisit
	bysort intervention: tab HBCevervisit
	glm HBCevervisit intervention, fam(bin) link(log) eform robust cluster(ward) difficult search

	svyset ward 
	svy: mean HBCevervisit if intervention == 1
	svy: mean HBCevervisit if intervention == 0

** How many times was the household visited by an HBC among those with HBCevervisit==1
   gen HBCvisitnum = .
   destring q31, replace
   replace HBCvisitnum = q31 if q31<99 & HBCevervisit ==1
   sum HBCvisitnum if HBCevervisit ==1 & intervention==1, detail
   sum HBCvisitnum if HBCevervisit ==1 & intervention==0, detail
   svy: mean HBCvisitnum if HBCevervisit ==1 & intervention==1
   svy: mean HBCvisitnum if HBCevervisit ==1 & intervention==0

	
/******************************************************************************						
		2. Frequencies and outcomes (Risk ratios, missing data)
******************************************************************************/

**ANC
** Never attended ANC - full population

	mdesc q12r
	bysort intervention: mdesc q12r
	bysort intervention: tab q12r
	glm q12r intervention, fam(bin) link(log) eform robust cluster(ward) difficult search

	
** Attended ANC <4 times - full population	
	// reverse code anc4more
	gen anc4less = .
	replace anc4less = 1 if anc4more==0 
	replace anc4less = 0 if anc4more==1
	
	mdesc anc4less 
	bysort intervention: mdesc anc4less
	bysort intervention: tab anc4less
	glm anc4less intervention, fam(bin) link(log) eform robust cluster(ward) difficult search


	
** late to attend ANC  - full population
	gen lateANC = .
	replace lateANC = 1 if q13>3
	replace lateANC=0 if q13<=3
	replace lateANC=. if q13==.
	
	bysort intervention: tab lateANC
	mdesc lateANC
	bysort intervention: mdesc lateANC
	glm lateANC intervention, fam(bin) link(log) eform robust cluster(ward) difficult search


***Facility delivery and intent
	gen currentpregifd = currentpreg
	recode currentpregifd (3 4 = 0) if facdelr != .
	recode currentpregifd (3 4 = 1) if intendfacdelr != .
	
	//delivery
	mdesc facdelr if currentpreg != 1
	bysort intervention: mdesc facdelr if currentpreg != 1
	bysort intervention: tab facdelr if currentpreg != 1
	glm facdelr intervention if currentpreg != 1, fam(bin) link(log) eform robust cluster(ward) difficult search
	
	//intent
	mdesc intendfacdelr if currentpregifd == 1
	bysort intervention: mdesc intendfacdelr if currentpregifd == 1
	bysort intervention: tab intendfacdelr if currentpregifd == 1
	glm intendfacdelr intervention if currentpregifd == 1, fam(bin) link(log) eform robust cluster(ward) difficult search
	
	
**Maternal knowledge
	foreach var in q37cr q38cr q39cr q41cr q42cr q43cr {
		glm `var' intervention, fam(bin) link(log) eform robust cluster(ward) difficult search
		bysort intervention: tab `var'
		mdesc `var'
		di `"`: var label `var''"'
		//logistic `var' intervention, cluster(ward)
	}
	

/******************************************************************************						
		3. Calculating ICCs
******************************************************************************/

icc lateANC ward if ward != 4 & ward != 21 & ward != 22 & ward != 26
	
	
/******************************************************************************						
		4. Sensitivity analyses 
******************************************************************************/

******** Restricting ANC outcomes to those who are not currently pregnant

** Never attended ANC - restrict to women who are not currently pregnant
	mdesc q12r if currentpreg != 1
	bysort intervention: tab q12r if currentpreg != 1
	glm q12r intervention if currentpreg != 1, fam(bin) link(log) eform robust cluster(ward) difficult search
	logistic q12r intervention if currentpreg != 1, cluster(ward)



**Attended 4 or more ANC -  restrict to women who are not currently pregnant
	mdesc anc4less if currentpreg != 1
	bysort intervention: tab anc4less if currentpreg != 1
	glm anc4less intervention if currentpreg != 1, fam(bin) link(log) eform robust cluster(ward) difficult search
	logistic anc4less intervention if currentpreg != 1, cluster(ward)

**Late attend ANC -  restrict to women who are not currently pregnant
	mdesc lateANC if currentpreg != 1
	bysort intervention: tab lateANC if currentpreg != 1
	glm lateANC intervention if currentpreg != 1, fam(bin) link(log) eform robust cluster(ward) difficult search



******** Adjusting for age group and percent with secondary education
		
** Facility delivery - adjusted for age group and percent with secondary education
glm facdelr intervention i.age_grp i.secondaryed if currentpreg != 1, fam(bin) link(log) eform robust cluster(ward) difficult search
glm intendfacdelr intervention i.age_grp i.secondaryed if currentpregifd == 1, fam(bin) link(log) eform robust cluster(ward) difficult search

** ANC outcomes - adjusted for age group and percent with secondary education
glm anc4less intervention i.age_grp i.secondaryed, fam(bin) link(log) eform robust cluster(ward) difficult search
glm lateANC intervention i.age_grp i.secondaryed, fam(bin) link(log) eform robust cluster(ward) difficult search
glm q12r intervention i.age_grp i.secondaryed, fam(bin) link(log) eform robust cluster(ward) difficult search



******** Using logistic regression instead of log-binomial models
logistic facdelr intervention if currentpreg != 1, cluster(ward)
logistic intendfacdelr intervention if currentpregifd == 1, cluster(ward)
logistic anc4less intervention, cluster(ward)
logistic lateANC intervention, cluster(ward)
logistic q12r intervention, cluster(ward)




******** When excluding those with unclear pregnancy vs recent delivery status

drop if currentpreg == 3 | currentpreg == 4 

** Never attended ANC 
	bysort intervention: tab q12r
	glm q12r intervention, fam(bin) link(log) eform robust cluster(ward) difficult search

	
** Attended ANC <4 times 	
	bysort intervention: tab anc4less
	glm anc4less intervention, fam(bin) link(log) eform robust cluster(ward) difficult search

** late to attend ANC  
	bysort intervention: tab lateANC
	glm lateANC intervention, fam(bin) link(log) eform robust cluster(ward) difficult search


***Facility delivery and intent	
	//delivery
	bysort intervention: tab facdelr if currentpreg != 1
	glm facdelr intervention if currentpreg != 1, fam(bin) link(log) eform robust cluster(ward) difficult search
	
	//intent
	bysort intervention: tab intendfacdelr if currentpregifd == 1
	glm intendfacdelr intervention if currentpregifd == 1, fam(bin) link(log) eform robust cluster(ward) difficult search
	






